<?php

// ver: 5.6+
// [PHP大全](http://c.biancheng.net/view/6138.html)

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 1);
assert_options(ASSERT_QUIET_EVAL, 0);

// assert
function mute(...$vd) {
}

function vari() {
    echo "常量变量\n";

    $b = true;

    $i = 1234;
    $f = 1.234;
    $n = null;
    isset($n);

    mute($b);
}

function stri() {
    echo "字符串\n";
    $s = 'a\nb';
    $s = "a\nb";
    $s = "a{$s}b";
    $lines = <<< EOF
      a
      b
EOF;
    $s = "10:234";
    // $i = strpos($s,":");
    // $a = substr($s,0,$i);
    // $b = substr($s,$i+1);
    list($a,$b) = explode(':',$s);
    // echo "s={$i},{$a},{$b}\n";
    // assert($i==2);
    assert($a=='10');
    assert($b=='234');
    // echo time(),"\n";
    $t = 1624433310;
    // t->dayofmonth
    $t_str=date('d',$t);
    assert(intval($t_str) == 23);
}
function coll() {
    echo "数组集合\n";
    $arr = array(
        "a",
        2 => "b",
        "c",
        "d"=>"x",
    );
    foreach ($arr as $a) {
        mute($a);
    }

    mute($arr[0]);

    assert(isset($arr["d"]));

    $arr = ["a",2=>"b","d"=>"x"];
    assert(isset($arr["d"]));
    assert(($arr["e"]??"e")=="e");
    assert($arr["2"]=="b" && $arr[2]=="b");

    // unset($arr[1]);
    // $ks=array_keys($arr);
    // print_r($arr);
    // print_r($ks);
}
function proc() {
    echo "流程控制\n";
    $a = 0;
    $b = 2;

    if ($a > $b) {
        mute();
    } elseif ($a == $b) {
        mute();
    } else {
        mute();
    }

    for ($i = 1; $i <= 10; $i++) {
        mute($i);
    }

    switch ($i) {
        case 0:
            mute($i);
            break;
        case 1:
            mute($i);
            break;
        case 2:
            mute($i);
            break;
    }
}
function func() {
    echo "函数方法\n";
    function f($a,$b){
        return $a+$b;
    }
    $f2 = function($a,$b){
        return $a+$b;
    };

    $fn="f";

    assert(f(1,2) == 3);
    assert($fn(1,2)==3);
    assert($f2(1,2)==3);
}

function clas() {
    echo "类和对象\n";
    class A {
        public $a = 'a';
        public function f() {
            mute($this->var);
        }
    }
    class B extends A {
        public $b = 'b';
    }

    $a = new A();
    assert($a->a == 'a');
}

function test() {
    echo "测试","\n";
    setlocale(LC_TIME, 'en_US');
    // $tz=timezone_open("Asia/Kolkata");
    $ts=1621332894; //2021-05-18 18:14:54
    $ts+=5*3600+30*60;  //UTC+5:30
    $dtStr=gmstrftime("%Y-%m-%d %H:%M:%S",$ts);
    // gmstrftime()
    assert($dtStr == '2021-05-18 15:44:54');

    $t1=1621332894; //2021-05-18 15:44:54 印度时间
    $t2=1621358094; //2021-05-19 01:14:54 中国时间 2021-05-18 10:44:54 印度时间
    $t1+=5*3600+30*60;
    $t2+=5*3600+30*60;
    $t1=intval($t1/86400);
    $t2=intval($t2/86400);
    assert($t1 == $t2);

    function tryRun($func) {
        try {
            return ($func)();
        } catch (Exception $e) {
            print($e);
        }
        return "";
    }
    function r() {
        return "r";
    }
    assert(tryRun(r) == "r");
    tryRun(function () {
        //print("t\n");
    });
    function x($c) {
        return "{$c}";
    }
    assert(x("x")=="x");
    $xx="xx";
    tryRun(function () use ($xx) {
        assert(x($xx)=="xx");
    });
}

function main() {
    echo "Hellow, World!\n";
    vari();
    stri();
    coll();
    proc();
    func();
    clas();
    test();
}

main();